/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package evaluation;

import com.mxgraph.layout.*;
import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
import com.mxgraph.layout.orthogonal.mxOrthogonalLayout;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.swing.util.mxMorphing;
import com.mxgraph.util.mxEvent;
import com.mxgraph.util.mxEventSource.mxIEventListener;
import com.mxgraph.view.mxGraph;
import com.mxgraph.util.mxEventObject;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.util.mxConstants;
import com.mxgraph.view.mxGraph;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;
import javax.swing.JFrame;
import util.Pair;

/**
 *
 * @author phong
 */
public class GraphShower extends JFrame {

    mxGraphComponent graphComponent;

    public GraphShower(Graph g) {
        super("Semantic Graph");

        mxGraph graph = new mxGraph();
        Object parent = graph.getDefaultParent();

        graph.getModel().beginUpdate();

        Random rand = new Random();

        try {
            Map<Vertex, Object> vmap = new LinkedHashMap<Vertex, Object>();
            for (Vertex v : g.getVertices()) {
                
                Object o = null;
                
                switch (v.getType()) {
                    case Vertex.TYPE_LABEL: 
                        o = graph.insertVertex(parent, null, v.getLabel(), 100, 100, 50, 20, "shape=hexagon");
                        break;
                    case Vertex.TYPE_OPT:
                        o = graph.insertVertex(parent, null, v.getLabel(), 100, 100, 50, 20, "shape=rhombus");
                        break;
                    case Vertex.TYPE_PRED:
                        o = graph.insertVertex(parent, null, v.getLabel(), 100, 100, 80, 20, "shape=rectangle");
                        break;
                    case Vertex.TYPE_REF:
                        o = graph.insertVertex(parent, null, v.getLabel(), 100, 100, 50, 20, "shape=ellipse");
                        break;
                    default:
                        o = graph.insertVertex(parent, null, v.getLabel(), 100, 100, 50, 20, "shape=rectangle");
                }
                
                
                vmap.put(v, o);
            }

            for (Edge e : g.getEdges()) {
                Pair<Vertex, Vertex> vs = e.getVerices();
                Object v1 = vmap.get(vs.getFirst());
                Object v2 = vmap.get(vs.getSecond());
                graph.insertEdge(parent, null, e.getLabel(), v1, v2);

            }
        } finally {
            graph.getModel().endUpdate();
        }

        graphComponent = new mxGraphComponent(graph);
        getContentPane().add(graphComponent);
        setLayout();
    }

    public static void show(Graph g) {
        GraphShower frame = new GraphShower(g);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 320);
        frame.setVisible(true);
    }

    public mxIGraphLayout setLayout() {
        mxIGraphLayout layout = null;

        final mxGraph graph = graphComponent.getGraph();
        layout = new mxHierarchicalLayout(graph);

        Object cell = graph.getSelectionCell();

        if (cell == null
                || graph.getModel().getChildCount(cell) == 0) {
            cell = graph.getDefaultParent();
        }

        graph.getModel().beginUpdate();
        try {
            long t0 = System.currentTimeMillis();
            layout.execute(cell);

        } finally {
            mxMorphing morph = new mxMorphing(graphComponent, 20,
                    1.2, 20);

            morph.addListener(mxEvent.DONE, new mxIEventListener() {

                public void invoke(Object sender, mxEventObject evt) {
                    graph.getModel().endUpdate();
                }
            });

            morph.startAnimation();
        }
        return layout;

    }
}
